package GUI;

import java.util.Comparator;

import backend.Task;

/**This class does the comparisons for sorting Task objects.
 * @author Defeng
 *
 */
class TaskComparator implements Comparator {
  /** Constant for column tag */
  public static final int ID = 0;
  public static final int IMPORTANCE = 1;
  public static final int DUEDATE = 2;
  public static final int START = 3;
  public static final int DUE = 4;
  public static final int DESCRIPTION = 5;
  public static final int LABEL = 6;
  public static final int VENUE = 7;
  
  /** Constant for direction */
  public static final int ASCENDING = 0;
  public static final int DESCENDING = 1;

  private int column;
  private int direction;

  /**
   * Compares two Playerv2 objects
   * 
   * @param obj1 the first Playerv2
   * @param obj2 the second Playerv2
   * @return int
   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
   */
  public int compare(Object obj1, Object obj2) {
    int rc = 0;
    Task p1 = (Task) obj1;
    Task p2 = (Task) obj2;

    // Determine which field to sort on, then sort on that field
    switch (column) {
    case IMPORTANCE:
  	  String bool1 = "false";
  	  String bool2 = "false";
  	  if(p1.hasImportance()) bool1 = "true";
  	  if(p2.hasImportance()) bool2 = "true";
    	//rc = p1.hasImportance() == p2.hasImportance() ? -1 : 1;
  	rc = bool1.compareTo(bool2);
      break;
    case DUEDATE:
      rc = p1.getDateString("yymmdd").compareTo(p2.getDateString("yymmdd"));
      break;
    case START:
      rc = p1.getstrStartTime().compareTo(p2.getstrStartTime());
      break;
    case DUE:
    	rc = p1.getstrEndTime().compareTo(p2.getstrEndTime());
      break;
    case DESCRIPTION:
      rc = p1.getDescription().compareTo(p2.getDescription());
      break;
    case LABEL:
      rc = p1.getLabel().compareTo(p2.getLabel());
      break;
    case VENUE:
        rc = p1.getVenue().compareTo(p2.getVenue());
        break;
    }

    // Check the direction for sort and flip the sign if appropriate
    if (direction == DESCENDING) {
      rc = -rc;
    }
    return rc;
  }

  /**
   * Sets the column for sorting
   * 
   * @param column the column
   */
  public void setColumn(int column) {
    this.column = column;
  }

  /**
   * Sets the direction for sorting
   * 
   * @param direction the direction
   */
  public void setDirection(int direction) {
    this.direction = direction;
  }

  /**
   * Reverses the direction
   */
  public void reverseDirection() {
    direction = 1 - direction;
  }
}
